#pragma once
#include <iostream>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <cassert>
#include "../comm/Log.hpp"

namespace ns_model
{
    using namespace std;
    using namespace ns_Log;
    using namespace ns_util;

    const string question_list = "./question/questionlist.txt";
    const string question_path = "./question/";

    // 1 回文数 简单 1 5000
    class Question
    {
    public:
        string name;   // 题目编号
        string title;  // 题目名称
        string star;   // 难度 简单 中等 困难
        int cpu_limit; // 时间限制
        int mem_limit; // 空间限制
        string desc;   // 题目描述
        string hander; // 题目预设给用户在线编辑器的代码
        string tail;   // 题目的测试用例，需要和header拼接，形成完整代码
    };

    class Model
    {
    private:
        unordered_map<string, Question> questions;

    public:
        Model()
        {
            assert(LoadQuestion(question_list));
        }

        ~Model() {}

        bool LoadQuestion(const string &question_list)
        {
            ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(Fatal) << "加载题目列表失败" << "\n";
                return false;
            }

            string line;
            string sep = " ";
            while (getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SplitString(&tokens, line, sep);
                if (tokens.size() != 5)
                {
                    LOG(Warning) << "部分题目加载失败" << "\n";
                    continue;
                }
                Question q;
                q.name = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                string question_number_path = question_path;
                question_number_path += q.name;
                question_number_path += "/";

                Fileutil::ReadFile(question_number_path + "desc.txt", &(q.desc), true);
                Fileutil::ReadFile(question_number_path + "hander.cpp", &(q.hander), true);
                Fileutil::ReadFile(question_number_path + "tail.cpp", &(q.tail), true);
                questions.insert({q.name, q});
            }
            LOG(Info) << "题目列表加载成功" << "\n";
            in.close();
            return true;
        }

        bool GetAllQuestion(vector<Question> *list)
        {
            if (questions.size() == 0)
            {
                LOG(Error) << "用户获取题目列表失败" << "\n";
                return false;
            }
            for (auto &iter : questions)
            {
                (*list).push_back(iter.second);
            }
            return true;
        }

        bool GetOneQuestion(string name, Question *out)
        {
            auto iter = questions.find(name);
            if (iter == questions.end())
            {
                LOG(Error) << "用户获取题目失败，题目编号" << name << "\n";
                return false;
            }
            (*out) = iter->second;
            return true;
        }
    };
}
